package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.GoodsService;
import com.atguigu.gmall.model.list.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/3 10:56 星期一
 * description: 商品相关的控制层(es)
 */
@RestController
@RequestMapping(value = "/api/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

   /* @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;*/

    /**
     * 创建索引 映射 类型 (手动创建 Springboot会自动创建)
     * @return
     */
  /*  @GetMapping(value = "/createIndexAndMapping")
    public Result createIndexAndMapping(){
        //创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        //创建映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        //返回
        return Result.ok();
    }*/

    /**
     * 根据skuId将数据库商品新增至ES
     * @param skuId
     * @return
     */
    @GetMapping(value = "/add/{skuId}")
    public Boolean add(@PathVariable(value = "skuId") Long skuId){
        goodsService.addGoodsFromDbToEs(skuId);
        return true;
    }

    /**
     * 根据skuId移除ES中的商品
     * @param goodsId
     * @return
     */
    @GetMapping(value = "/remove/{goodsId}")
    public Boolean remove(@PathVariable(value = "goodsId")Long goodsId){
        goodsService.remove(goodsId);
        return true;
    }

    /**
     * 为商品增加热度值
     * @param goodsId
     * @return
     */
    @GetMapping(value = "/addScore/{goodsId}")
    public Result addScore(@PathVariable(value = "goodsId")Long goodsId){
        goodsService.addScore(goodsId);
        return Result.ok();
    }

}
